import React, { Component } from 'react';
import { WeaSearchGroup, WeaTop, WeaFormItem, WeaLocaleProvider } from 'ecCom';
import { WeaForm, WeaSwitch } from 'comsMobx';
import { Button, message } from 'antd';
import CarConstant from '../../util/CarConstant';
import { inject, observer } from 'mobx-react';
import NoRight from '../NoRight';

const getLabel = WeaLocaleProvider.getLabel;
@inject('carApplySettingStore')
@observer
class CarApplySetting extends Component {
  constructor(props) {
    super(props);
    this.state = { isRight: 2 };
    this.saveInfo = this.saveInfo.bind(this);
    this.setIsRight = this.setIsRight.bind(this);
  }

  componentDidMount() {
    const { carApplySettingStore } = this.props;
    carApplySettingStore.initCarApplySettingForm(this.setIsRight);
  }
  setIsRight(isRight) {
    this.setState({ isRight });
  }
  buildForm() {
    const { carApplySettingStore } = this.props;
    const { form, conditioninfo } = carApplySettingStore;
    const { isFormInit } = form;
    const group = [];
    isFormInit && conditioninfo.slice().map((c, index) => {
      const items = [];
      c.items.map((fields) => {
        items.push({
          com: (<WeaFormItem
            ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@qrgl6r@${fields.domkey[0]}`}
            label={`${fields.label}`}
            labelCol={{ span: `${fields.labelcol}` }}
            wrapperCol={{ span: 10 }}
          >
            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@w9v6cq@${fields.domkey[0]}`} fieldConfig={fields} form={form} onChange={(data) => {}} />
          </WeaFormItem>),
          colSpan: 1,
        });
      });
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@sxx8yo@${index}`} col={1} needTigger title={c.title} showGroup={c.defaultshow} items={items} />);
    });
    return group;
  }

  saveInfo() {
    const { carApplySettingStore } = this.props;
    const { form } = carApplySettingStore;
    carApplySettingStore.saveCarApplySetting(form.getFormParams());
  }

  render() {
    if (this.state.isRight == 1) {
      return (
        <div>
          <WeaTop
            ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@xesx5w`}
            title={getLabel(31811, '应用设置')}
            loading
            icon={CarConstant.icon}
            iconBgcolor={CarConstant.iconBgColor}
            buttons={[
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@y40tn3@saveInfo`} type="primary" onClick={this.saveInfo}>{getLabel(86, '保存')}</Button>,
            ]}
          />
          {this.buildForm()}
        </div>
      );
    } else if (this.state.isRight == 0) {
      return (
        <div>
          <NoRight ecId={`${this && this.props && this.props.ecId || ''}_NoRight@g18nnl`} visible />
        </div>
      );
    }
    return <div />;
  }
}

export default CarApplySetting;
